package dk.dtu.android.transport.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class BaseDbAdapter {

	protected static final String DATABASE_NAME = "transport.db";
	public static final int DATABASE_VERSION = 3;

	protected static SQLiteDatabase db;
	protected static DatabaseHelper helper;
	
	public int _id;
	
	public BaseDbAdapter(){}
	
	public static void initialize(Context context)
	{
		if (db == null) 
		{
			helper = new DatabaseHelper(context);
			db = helper.getWritableDatabase();
		}
	}
	
	/* Table methods (static) */
	
	public static void filter()
	{
		
	}
	
	public static void all()
	{
		
	}
	
	public static void deleteAll()
	{
		//db.delete(TABLE_NAME, null, null);
	}
	
	/* Row methods (object) */
	
	public void save()
	{
		
	}
	
	public void delete()
	{
		
	}
	
	public static void customUpgrade(){
		db.execSQL("drop table if exists key_value");
		db.execSQL(KeyValueTable.CREATE_TABLE);
	}
	
	private static class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
	      }

	      @Override
	      public void onCreate(SQLiteDatabase db) {
	         db.execSQL(KeyValueTable.CREATE_TABLE);
	      }

	      @Override
	      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	         if (oldVersion < 2 && newVersion <= 2)
	         {
	        	 db.execSQL(PendingRequestTable.CREATE_TABLE);
	         }
	         if (oldVersion < 3 && newVersion <= 3)
	         {
	        	 db.execSQL(StopstationTable.CREATE_TABLE);
	         }
	      }
	   }
	
}
